<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="公司名称">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="公司状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司类型">
        <el-select v-model="queryParams.deptType" placeholder="公司类型" clearable size="small">
          <el-option
            v-for="dict in Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="deptList"
      row-key="deptId"

      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!--       default-expand-all   -->
      <el-table-column prop="deptName" label="公司名称" width="200"></el-table-column>
      <el-table-column prop="orderNum" label="排序" width="200"></el-table-column>
      <el-table-column prop="status" label="状态" :formatter="statusFormat" width="200"></el-table-column>
      <el-table-column prop="status" label="公司类型" :formatter="Format" width="200"></el-table-column>
      <el-table-column label="余额" align="center"  width="200" prop="money" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleMoney(scope.row)">
            {{scope.row.money}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="bankCard" label="银行卡号" width="200"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dept:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:dept:add']"
          >新增</el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="550px">
      <el-table
        :data="moneyList"
        style="width: 100%">
        <el-table-column
          prop="money"
          label="余额"
          width="150">
        </el-table-column>
        <el-table-column
          prop="moneyType"
          label="变更类型"
          width="150"
          :formatter="statusFormat2"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="200">
        </el-table-column>

      </el-table>
      <pagination
        v-show="total1>0"
        :total="total1"
        :page.sync="moneyParams.pageNum"
        :limit.sync="moneyParams.pageSize"
        @pagination="handleMoney"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确定</el-button>
      </div>

    </el-dialog>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open"   top="15px" width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>

          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级公司" prop="parentId">
              <treeselect v-model="form.parentId" :options="deptOptions" placeholder="选择上级公司" />
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="银行卡号" prop="bankCard">
              <el-input v-model="form.bankCard" placeholder="请输入银行卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum"  :min="1" :max="200" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司类型">
              <el-radio-group v-model="form.deptType">
                <el-radio
                  v-for="dict in Options"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .map {
    width: 100%;
    height: 400px;
  }
</style>
<script>
  import { listDept, getDept,moneyList, listAttr,initCategory,treeselect, delDept, addDept, updateDept } from "@/api/system/dept";
  import { getToken } from '@/utils/auth'
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 表格树数据
        deptList: [],
        moneyList:[],
        moneyOptions:[],
        // 部门部门树选项
        deptOptions: undefined,
        Options:undefined,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open2:false,
        total1:'0',
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        moneyParams:{
          deptId: undefined,
          pageNum: 1,
          pageSize: 10,
        },
        queryParams: {
          deptName: undefined,
          status: undefined
        },
        // 表单参数
        form: {
          mapCenter: { lng: 0, lat: 0 },
          fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
        },
        // 表单校验
        rules: {
          parentId: [
            { required: true, message: "上级公司不能为空", trigger: "blur" }
          ],
          deptName: [
            { required: true, message: "公司名称不能为空", trigger: "blur" }
          ],
          orderNum: [
            { required: true, message: "菜单顺序不能为空", trigger: "blur" }
          ]
        },
        uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        hackReset:false,
        mapZoom: 15,
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("dept_type").then(response => {
        this.Options = response.data;
      });
      this.getDicts("sys_money_type").then(response => {
        this.moneyOptions = response.data;
      });
    },
    methods: {

      handleMoney(row){
        this.moneyParams.deptId =row.deptId
        moneyList(this.moneyParams).then(response => {
          console.log(response)
          this.moneyList = response.rows;
          this.open2 = true;
          this.total1 = response.total;
          this.title = "查看余额变动";
        })
      },


      /** 查询部门列表 */
      getList() {
        this.loading = true;
        listDept(this.queryParams).then(response => {
          console.log(response)
          this.deptList = response.data;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          console.log('查询公司下拉树结构',response.data)
          this.deptOptions = response.data;
        });
      },
      // 字典状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      Format(row, column) {
        return this.selectDictLabel(this.Options, row.deptType);
      },
      statusFormat2(row, column) {
        return this.selectDictLabel(this.moneyOptions, row.moneyType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.open2 = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          deptId: undefined,
          parentId: 100,
          deptName: undefined,
          status: "0",
          deptType:"1",
          bankCard:undefined
        };
        this.resetForm("form");
      },
      tableRowClassName(row, index) {
        if (index === 1) {
          return 'info-row'
        } else if (index === 3) {
          return 'positive-row'
        }
        return ''
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.hackReset = false
        this.$nextTick(() => {
          this.hackReset = true
        })
        this.getTreeselect();
        if (row != undefined) {
          this.form.parentId = row.deptId;
        }
        this.open = true;
        this.title = "添加公司";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        getDept(row.deptId).then(response => {
          this.form = response.data;
          this.hackReset = false;
          this.$nextTick(() => {
            this.hackReset = true
          })
          this.open = true;
          this.title = "修改公司";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.deptId != undefined) {
              updateDept(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addDept(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除名称为"' + row.deptName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDept(row.deptId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      }
    }
  };
</script>
